Cant access NString after callback in [NSURLConnection sendSynchronousRequest]

Posted by John ClearZ on Stack Overflow See other posts from Stack Overflow or by John ClearZ
Published on 2010-05-13T22:56:36Z Indexed on 2010/05/14 2:44 UTC
Read the original article Hit count: 175

Filed under:
|
|

Hi I am trying to get a cookie from a site which I can do no problem. The problem arises when I try and save the cookie to a NSString in a holder class or anywhere else for that matter and try and access it outside the delegate method where it is first created.

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
 {
  int i;
  NSString* c;
     NSArray* all = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[NSURL URLWithString:@"http://johncleary.net"]];
  //NSLog(@"RESPONSE HEADERS: \n%@", [response allHeaderFields]);
  for (i=0;i<[all count];i++)
   {
    NSHTTPCookie* cc = [all objectAtIndex: i];
    c = [NSString stringWithFormat: @"%@=%@", [cc name], [cc value]];
    [Cookie setCookie: c];
   NSLog([Cookie cookie]) // Prints the cookie fine.

  }



  [receivedData setLength:0];
 }

I can see and print the cookie when I am in the method but I cant when trying to access it form anywhere else even though it gets stored in the holder class

@interface Cookie : NSObject
{
 NSString* cookie;
}
+ (NSString*) cookie;
+ (void) setCookie: (NSString*) cookieValue;
@end

int main (void)
{
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 JCLogin* login;
 login = [JCLogin new];

 [login DoLogin];
 NSLog([Cookie cookie]); // Crashes the program
 [pool drain];
 return 0;

}

© Stack Overflow or respective owner

Related posts about nsurlconnection

Related posts about objective-c